package com.demo.connectdbtest.module.ssh.controller;

import ch.ethz.ssh2.Connection;
import com.jcraft.jsch.ChannelSftp;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.JSchException;
import com.jcraft.jsch.Session;
import com.jcraft.jsch.SftpException;

public class FileUploadHandler {

    /**
     * test success
     * @param args
     */
    public static void main(String[] args) {

        //本地文件的路径
        String localFilePath = "D:\\workData\\pythonProject\\20230913project\\traffic_brain\\shell\\jacoco_dev.sh";
        //远程服务器上存储文件的路径
        String remoteFilePath = "/data/learn/jacoco_dev.sh";
        //远程服务器IP
        String remoteHost = "10.252.176.46";
        ;
        //远程服务器SSH端口
        int port = 22;
        //远程服务器用户名
        String username = "root";
        //远程服务器密码
        String password = "Anjian@20230411^";
        Connection conn = new Connection("10.252.176.46");

        try {
            JSch jsch = new JSch();
            Session session = jsch.getSession(username, remoteHost, port);
            session.setPassword(password);
            session.setConfig("StrictHostKeyChecking", "no"); // Disable host key checking
            session.connect();
            ChannelSftp channelSftp = (ChannelSftp) session.openChannel("sftp");
            channelSftp.connect();
            channelSftp.put(localFilePath, remoteFilePath);
            channelSftp.disconnect();
            session.disconnect();
            System.out.println("File uploaded successfully.");
        } catch (JSchException | SftpException e) {
            e.printStackTrace();
        }
    }
}



